using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TuyennvLibrary.SqlClient;
namespace TuyennvLibrary.ServerApi.Models
{
    public class TypeApplication
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string NameUnsign { get; set; }
        public int Status { get; set; }


        public static string COLUMN_ID = "Id";
        public static string COLUMN_NAME = "Name";
        public static string COLUMN_NAMEUNSIGN = "NameUnsign";
        public static string COLUMN_STATUS = "Status";


        public static string TableName = "TypeApplication";
        public TypeApplication()
        {
            this.Id = 0;
            this.Name = string.Empty;
            this.NameUnsign = string.Empty;
            this.Status = 0;

        }
    }
    public class TypeApplicationModel : BaseModel<TypeApplication>
    {
        public TypeApplicationModel()
        {
            table = TypeApplication.TableName;
        }
        public bool Insert(TypeApplication obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter(TypeApplication.COLUMN_NAME,obj.Name),
					new SqlParameter(TypeApplication.COLUMN_NAMEUNSIGN,obj.NameUnsign),
					new SqlParameter(TypeApplication.COLUMN_STATUS,obj.Status),
                    
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Update(TypeApplication obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter(TypeApplication.COLUMN_NAME,obj.Name),
					new SqlParameter(TypeApplication.COLUMN_NAMEUNSIGN,obj.NameUnsign),
					new SqlParameter(TypeApplication.COLUMN_STATUS,obj.Status),

                }, new SqlParameter[]{
					new SqlParameter(TypeApplication.COLUMN_ID,obj.Id),
															 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Delete(int Id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter(TypeApplication.COLUMN_ID,Id),
															 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string ListID)
        {
            try
            {
                if (ListID != string.Empty)
                {
                    foreach (string ID in ListID.Split(','))
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<TypeApplication> GetByPage(int page, int pagesize, List<TypeApplication> source)
        {
            List<TypeApplication> list = new List<TypeApplication>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<TypeApplication> GetListBy(DataTable dt)
        {
            List<TypeApplication> list = new List<TypeApplication>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TypeApplication obj = GetBy(dr);
                    list.Add(obj);
                }
            }
            return list;
        }
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable Search(string name, int status)
        {
            string query = "select * from " + table + " where 1=1";
            if (!string.IsNullOrEmpty(name))
            {
                query += string.Format(" and (name like N'%{0}%' or nameunsign like '%{1}%'", name, name.GetUnsignText());
            }
            if (status != 2)
            {
                query += string.Format(" and status={0}", status);
            }
            query += " order by name";
            return DB.ExecuteDataTable(query, null);
        }
        public TypeApplication GetBy(DataRow dr)
        {
            try
            {
                TypeApplication obj = new TypeApplication();
                obj.Id = (dr[TypeApplication.COLUMN_ID] is DBNull) ? 0 : int.Parse(dr[TypeApplication.COLUMN_ID].ToString());
                obj.Name = (dr[TypeApplication.COLUMN_NAME] is DBNull) ? string.Empty : dr[TypeApplication.COLUMN_NAME].ToString();
                obj.NameUnsign = (dr[TypeApplication.COLUMN_NAMEUNSIGN] is DBNull) ? string.Empty : dr[TypeApplication.COLUMN_NAMEUNSIGN].ToString();
                obj.Status = (dr[TypeApplication.COLUMN_STATUS] is DBNull) ? 0 : int.Parse(dr[TypeApplication.COLUMN_STATUS].ToString());

                return obj;
            }
            catch (Exception)
            {
            }

            return new TypeApplication();
        }
        public TypeApplication GetBy(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    return GetBy(dr);
                }
            }
            return new TypeApplication();
        }
        public TypeApplication GetByID(int Id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter(TypeApplication.COLUMN_ID,Id),
															
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
